<?php
/**
 * Base Data Accessor object for CTRT_RaidTracker
 *
 * @category Plugins
 * @package CT_RaidTrackerImport
 * @copyright (c) 2006, EQdkp <http://www.eqdkp.com>
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 *
 * @author Garrett Hunter <loganfive@blacktower.com>
 * $Rev: 109 $ $Date: 2007-05-05 02:58:24 +0000 (Sat, 05 May 2007) $
 */

if ( !defined('EQDKP_INC') )
{
    die('Haxor!!!');
}

include_once("config_ctrt.php");

/**
 * Class for managing data access
 * @subpackage CTRTDataAccessors
 */
class CTRT_DataAccessor {

    /**
     * The name of the table upon which I am operating
     */
    var $_myTable;
    var $_myPrimaryKey;

    function CTRT_DataAccessor () { }

    /**
     * Get a single record for the object based on either its ID or NAME
     * @var int $cmd Indicates how the data should be retrieved
     * @var mixed $param The specific record to retieve
     */
    function getRecord ($cmd=CTRT_GET_ALL,$param="") {}

    /**
     * Insert a record into the database
     * @var string $table
     * @var array $data
     */
    function doInsert($data) {
        global $db;

        $query = $db->build_query('INSERT', $data);
        $db->query('INSERT INTO ' . $this->getMyTable() . $query);

        return 1;
    }

    /**
     * Update a record
     * @var $data Contains all column names and values to be updated
     */
     function doUpdate($key, $data) {
        global $db;

        // Update the member
        $query = $db->build_query('UPDATE', $data);
        $db->query('UPDATE ' . $this->getMyTable() . ' SET ' . $query . " WHERE ".$this->getMyPrimaryKey()."=" . $key);

        return 1;
    }

    /**
     * Delete a record by its ID
     */
    function deleteById($id) {
        global $db;

        $db->query('DELETE FROM ' . $this->getMyTable() . " WHERE ".$this->getMyPrimaryKey()."=" . $id . "");

        return 1;
    }


    /**
     * Get all records for the object
     */
    function getAll () {
        global $db;

        return $this->getRecord(CTRT_GET_ALL);
    }

    /**
     * Get a single record for the object based on it's ID
     * @var integer $id The id of the record to retrieve
     */
    function getByID ($id) {
        global $db;

        if (isset($id) && is_numeric($id)) {
            return $this->getRecord(CTRT_GET_BY_ID, $id);
        } else {
            return 0;
        }
    }

    /**
     * Get a single record for the object based on it's ID
     * @var integer $id The id of the record to retrieve
     */
    function getByName ($name) {
        global $db;

        if (isset($name)) {
            return $this->getRecord(CTRT_GET_BY_NAME, mysql_real_escape_string($name));
        } else {
            return 0;
        }
    }

    /**
     * Get a single record for the object based on it's ID
     */
    function getInsertId () {
        global $db;

        return $db->insert_id();
    }

    /**
     * Cleans and capitalizes a word
     */
    function capitalize($word) {
        // Make sure the alias name is properly capitalized
        $word = strtolower(preg_replace('/[[:space:]]/i', ' ', $word));
        return ucwords(trim($word));
    }

    //========================================
    // Getter / Setter methods
    //========================================

    /**
     * Get /Set the name of my table
     */
    function setMyTable($table_name) { $this->_myTable = $table_name; return 1; }
    function getMyTable() { return $this->_myTable; }

    /**
     * Get / Set the primary key
     */
    function setMyPrimaryKey($key_name) { $this->_myPrimaryKey = $key_name; return 1; }
    function getMyPrimaryKey() { return $this->_myPrimaryKey; }
}
?>